import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:get/get.dart';

import 'modify_sex_state.dart';

class ModifySexLogic extends GetxController {
  final ModifySexState state = ModifySexState();

  bool isInit = Get.arguments?['isInit'] ?? false;

  int initSex = 0;

  @override
  void onInit() {
    super.onInit();
    initSex = AppUserManager().user.value?.sex ?? 0;
    if (isInit == false) {
      state.isGirl.value = initSex == 0;
    }

  }

  void chooseSex(bool chooseGirl) {
    state.isGirl.value = chooseGirl;
    if (state.canChange.value == false) {
      state.canChange.value = true;
    }
  }

  void saveSex() async {
    if (state.isGirl.value == null) {
      return;
    }
    int gender = state.isGirl.value == true ? 0 : 1;
    if (gender == initSex) {
      Get.back();
    } else {
      var response =
          await AppUserManager().saveUserInfo(sex: gender.toString(), showLoading: true);
      if (response.isSuccess) {
        Get.back();
      } else {
        LoadingUtils.showToast(response.codeAndMsg);
      }
    }
  }
}
